<template>
    <div class="purchase-order">
      <div class="search-condition">
        <el-form label-width="120px">
          <el-form-item label="单位名称">
            <el-input
              v-model="input"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label-width="50px">
            <el-button type="primary" @click="pageNav(1,5)">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <AddGoodUnit @childClick="pageNav(1,5)"></AddGoodUnit>
      <el-table
            :data="tableData"
            border
            style="width: 100%"
        >
        <el-table-column label="序号" width="150">
            <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
        </el-table-column>
        <el-table-column prop="unitName" label="单位名称" width="120">
        </el-table-column>
        <el-table-column prop="createName" label="创建人" width="120">
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="220">
        </el-table-column>
        <el-table-column prop="modifyName" label="修改人" width="100"> </el-table-column>
        <el-table-column prop="modifyDate" label="修改时间" width="220">
        </el-table-column>
        <el-table-column label="操作">
        <template v-slot="scope">
            <UpdateGoodUnit @childClick="pageNav(1,5)" :unitId="scope.row.id"/>
            <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
        </el-table-column>
    </el-table>
      <div class="block">
        <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </div>
  </template>
    
    <script>
    import AddGoodUnit from "./AddGoodUnit.vue";
    import UpdateGoodUnit from "./UpdateGoodUnit.vue";
  export default {
    name: "goodUnit",
    components: {
        AddGoodUnit,
        UpdateGoodUnit
    },
    data() {
      return {
        input: "",
        tableData: [],
        total:0,
        currentPage:1,
        pageSize:5,
      };
    },
    methods:{
        handleEdit(index, row) {
            console.log(index, row);
        },
        handleDelete(index, row) {
            this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }).then(() => {
              console.log(index, row);
              console.log(row.id);
              this.$http({
                url: "http://localhost:8086/delUnit",
                method: "post",
                params: {
                    id: row.id
                },
              }).then((response) => {
                console.log(response.data);
                    if (response.data.code == "200") {
                        this.$message({
                        type: "success",
                        message: "删除成功!",
                        });
                    }else{
                        this.$message({
                        type: "error",
                        message: response.data.msg,
                        })
                    }
                this.pageNav(this.currentPage,5);
              }).catch((err) => {
                console.log(err);
              });
            }).catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        },
        pageNav(pageNum,pageSize){
            this.$http({
                url:"http://localhost:8086/queryUnitByPage",
                method:"post",
                params:{
                    pageSize,
                    pageNum,
                    unitName:this.input
                }
            }).then((response) => {
                this.tableData = response.data.data.list;
                this.total = response.data.data.total;
                this.currentPage = pageNum;
            }).catch((err) => {
                console.log(err);
            });
        },
        handleCurrentChange(currentPage) {
            this.pageNav(currentPage,5);
        }
    },
    mounted() {
        this.pageNav(1,5);
    }
  };
  </script>
    
  <style>
  .search-condition {
    display: flex;
    margin: 10px 0;
  }
  .search-condition > form {
    display: flex;
  }
  .search-condition input {
    width: 200px;
  }
  .block{
    text-align: right;
   
  }
  </style>
    